<?php 
/**
  * 广告控制器
  * date 2013-6-15 
  */
class AdAction extends CommonAction {

  private $uploadType='ad';

	public function _initialize()
	{   
    $this->currentModel = D(MODULE_NAME);
    $this->AdPositionModel = D('AdPosition');
    $allPosition=$this->AdPositionModel->field('id,title')->where(array('status'=>1))->select();
    $this->assign('allPosition',$allPosition);
		parent::_initialize();		
	}

  public function index()
  {
    $position=$this->_checkAdPosition();
    $map=$this->_search();
    $map['position_id']=$position['id'];
    $this->_filter($map);
    $count=$this->currentModel->where($map)->count();
    $page=$this->_page($count,C('PAGE_SUM'));
    $datas=$this->currentModel->where($map)->limit($page->firstRow.','.$page->listRows)->order($this->pkId.' DESC')->select();
    $this->assign('page',$page->show());
    $this->assign('datas',$datas);
    $this->assign('currentPosition',$position);       
    $this->display();
  }

/**
* 过滤搜索条件
* @access private
* @param array &$map
*/
  private function _filter(&$map)
  {
    $this->assign('param',$map);
        if(isset($map['title']))
        {
          $map['title']=array('like',"%".$map['title']."%");
        }
        if((isset($_REQUEST['start_time'])&&$_REQUEST['start_time']>0)&&(isset($_REQUEST['end_time'])&&$_REQUEST['end_time']>0))
        {
          $map['add_time']=array('BETWEEN',strtotime($_REQUEST['start_time']).','.strtotime($_REQUEST['end_time']));
        }elseif(isset($_REQUEST['start_time'])&&$_REQUEST['start_time']>0)
        {
          $map['add_time']=array('EGT',strtotime($_REQUEST['start_time']));
        }elseif(isset($_REQUEST['end_time'])&&$_REQUEST['end_time']>0)
        {
          $map['add_time']=array('ELT',strtotime($_REQUEST['end_time']));
        }       
  }  


/**
* 新增广告
*/
  public function add()
  {
    if($this->isPost())
    {
      $this->_save();
    }else
        {
          $position=$this->_checkAdPosition();
          $this->assign('currentPosition',$position);
          $this->display();
        }
  }

/**
* 编辑广告
*/
  public function edit()
  {
    if($this->isPost())
    {
      $this->_save();
    }else
        {
          $position=$this->_checkAdPosition();
          $this->assign('currentPosition',$position);        
          $id=$this->_get($this->pkId);
          $data=$this->currentModel->find($id);
          if(empty($data))$this->error('非法操作',U(MODULE_NAME.'/index'));
          $this->assign('data',$data);
          $this->display('add');
        }
  }

/**
* 删除广告
* @access public
*/
  public function del()
  {
    $position=$this->_checkAdPosition();
    $id=$this->_request($this->pkId);
    if(is_array($id))$id=implode(',',$id);
    if($this->currentModel->delete($id)>0)
    {
      $this->success('成功',U(MODULE_NAME.'/index',array('position_id'=>$position['id'])));
    }else
      {
        $this->error('失败',U(MODULE_NAME.'/index',array('position_id'=>$position['id'])));
      }
  }

  private function _save()
  {
      $this->_uploadPic($_POST['width'],$_POST['height']);
      if($this->currentModel->create()===FALSE)
      {
        $this->error($this->currentModel->getError());
      }
      $id=$this->_post($this->pkId);
      $funcName=$id>0?'save':'add';
      if($this->currentModel->$funcName()!==false)
      {
        $this->success('成功',U(MODULE_NAME.'/index',array('position_id'=>$_POST['position_id'])));
      }else
          {
            $this->delUploadFile($this->uploadType,$_POST['pic']);
            $this->error('失败');
          }      
  }

  /**
  * 改变信息状态
  * @access public
  */
  public function change()
  {
    $position=$this->_checkAdPosition();
    $id=$this->_post($this->pkId);
    if(empty($id))$this->error('请选择操作信息',U(MODULE_NAME.'/index',array('position_id'=>$position['id'])));
    $funcName=$this->_get('type');
    if(method_exists($this->currentModel,$funcName))
    {
      foreach($id as $v)
      {
        $this->currentModel->$funcName(array('id'=>$v));
      }
    }else{$this->error('非法操作',U(MODULE_NAME.'/index',array('position_id'=>$position['id'])));}
    $this->success('成功',U(MODULE_NAME.'/index',array('position_id'=>$position['id'])));
  }  

/**
* 上传图片
* @access private
*/
  private function _uploadPic($width,$height)
  {
    $config['width']=$width;
    $config['height']=$height;
    import('@.ORG.MyUploadFile');
    $upload=new MyUploadFile($this->uploadType,$config,false);
    $upload->run_upload();
    $picInfo=$upload->getUploadFileInfo();
    if(is_array($picInfo))
    {
      $_POST['pic']=$picInfo[0]['savename'];
    }
  }

  private function _checkAdPosition()
  {
    $positionId=$this->_get('position_id');
    if((int)$positionId<=0)
    {
      $this->error('非法操作',U('AdPosition/index'));
    }else
        {

          return $this->AdPositionModel->find($positionId);
        }
  
  }      


}	
 ?>